<script>
import {getToken} from '@/utils/auth';
import app_upgrade from '@/uni_modules/app-upgrade/js_sdk/index'
import {getValidVersion} from "@/api/wms/version";
import {whiteList} from "@/permission";

export default {
  globalData: {
    isLogin: false,
    currentTab: 0 // 添加当前选中的tab索引
  },
  onLaunch: function () {
    console.log('App Launch')
    this.initApp()

    // #ifdef H5
    window.onhashchange = () => {
      this.initApp()
    }
    // #endif

    // #ifdef APP-PLUS
    app_upgrade(async (wgtinfo) => {
      //查询是否更新
      const res = await getValidVersion(wgtinfo.name)
      if (res.code == 200) {
        return {
          changelog: res.data.remark,
          status: res.data.versionCode == wgtinfo.versionCode ? 0 : 1, // 0 无新版本 | 1 有新版本
          path: res.data.ossVoList[0].url // 新apk地址
        }
      }
    })
    // #endif

    // 初始化当前页面路径
    const pages = getCurrentPages()
    if (pages.length > 0) {
      const route = pages[pages.length - 1].route
      this.updateCurrentTab(route)
    }
  },
  onShow: function () {
    console.log('App Show')
    
    // 获取当前页面路径
    const pages = getCurrentPages()
    if (pages.length > 0) {
      const route = pages[pages.length - 1].route
      this.updateCurrentTab(route)
      
      // 发送tab变化事件
      uni.$emit('tabChange', { path: route })
    }
    
    // 检查登录状态
    this.checkLogin()
  },
  onHide: function () {
    console.log('App Hide')
  },
  methods: {
    initApp() {
      // 检查用户登录状态
      this.checkLogin()
    },
    checkLogin() {
      const token = uni.getStorageSync('token')
      this.globalData.isLogin = !!token

      if (!getToken()) {
        // #ifdef H5
        const href = window.location.href
        const toURL = href.split('#')[1]
        if (!whiteList.includes(toURL)) {
          uni.reLaunch({
            url: '/pages/login'
          })
        }
        // #endif
      }
    },
    // 更新当前选中的tab索引
    updateCurrentTab(route) {
      const tabRoutes = [
        'pages/index',
        'pages/sellment/cart/index',
        'pages/mine'
      ]
      
      const index = tabRoutes.findIndex(item => route.includes(item))
      if (index !== -1) {
        this.globalData.currentTab = index
        // 更新页面标题
        const titles = ['商品', '购物车', '我的']
        uni.setNavigationBarTitle({
          title: titles[index]
        })
      }
    }
  }
}
</script>

<template>
  <!-- 不在根组件添加自定义Tabbar，由各页面单独引入 -->
</template>

<style lang='scss'>
@import 'tailwindcss/base';
@import 'tailwindcss/utilities';
@import 'tailwindcss/components';

@import '@/static/scss/index.scss';
@import "uview-plus/index.scss";

/* 全局背景气泡样式 */
page {
  background: linear-gradient(150deg, #fcf9f7 0%, #fff 100%);
  position: relative;
  z-index: 1;
  
  /* 装饰气泡元素 - 右上角大气泡 */
  &::before {
    content: '';
    position: fixed;
    top: -30rpx;
    right: -30rpx;
    width: 280rpx;
    height: 280rpx;
    border-radius: 50%;
    background: linear-gradient(135deg, rgba(255, 107, 0, 0.1), rgba(255, 123, 0, 0.06));
    z-index: 0;
  }
  
  /* 装饰气泡元素 - 左下角小气泡 */
  &::after {
    content: '';
    position: fixed;
    bottom: 200rpx;
    left: -70rpx;
    width: 220rpx;
    height: 220rpx;
    border-radius: 50%;
    background: linear-gradient(135deg, rgba(255, 107, 0, 0.08), rgba(255, 123, 0, 0.04));
    z-index: 0;
  }
}

/* 动画效果定义 */
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes slideUp {
  from { 
    opacity: 0;
    transform: translateY(20rpx);
  }
  to { 
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-15rpx); }
}

/*
  全局公共样式和字体图标
*/
@font-face {
  font-family: yticon;
  font-weight: normal;
  font-style: normal;
  src: url('https://at.alicdn.com/t/font_1078604_w4kpxh0rafi.ttf') format('truetype');
}

.yticon {
  font-family: "yticon" !important;
  font-size: 16px;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.icon-yiguoqi1:before {
  content: "\e700";
}

.icon-iconfontshanchu1:before {
  content: "\e619";
}

.icon-iconfontweixin:before {
  content: "\e611";
}

.icon-alipay:before {
  content: "\e636";
}

.icon-shang:before {
  content: "\e624";
}

.icon-shouye:before {
  content: "\e626";
}

.icon-shanchu4:before {
  content: "\e622";
}

.icon-xiaoxi:before {
  content: "\e618";
}

.icon-jiantour-copy:before {
  content: "\e600";
}

.icon-fenxiang2:before {
  content: "\e61e";
}

.icon-pingjia:before {
  content: "\e67b";
}

.icon-daifukuan:before {
  content: "\e68f";
}

.icon-pinglun-copy:before {
  content: "\e612";
}

.icon-dianhua-copy:before {
  content: "\e621";
}

.icon-shoucang:before {
  content: "\e645";
}

.icon-xuanzhong2:before {
  content: "\e62f";
}

.icon-gouwuche_:before {
  content: "\e630";
}

.icon-icon-test:before {
  content: "\e60c";
}

.icon-icon-test1:before {
  content: "\e632";
}

.icon-bianji:before {
  content: "\e646";
}

.icon-jiazailoading-A:before {
  content: "\e8fc";
}

.icon-zuoshang:before {
  content: "\e613";
}

.icon-jia2:before {
  content: "\e60a";
}

.icon-huifu:before {
  content: "\e68b";
}

.icon-sousuo:before {
  content: "\e7ce";
}

.icon-arrow-fine-up:before {
  content: "\e601";
}

.icon-hot:before {
  content: "\e60e";
}

.icon-lishijilu:before {
  content: "\e6b9";
}

.icon-zhengxinchaxun-zhifubaoceping-:before {
  content: "\e616";
}

.icon-naozhong:before {
  content: "\e64a";
}

.icon-xiatubiao--copy:before {
  content: "\e608";
}

.icon-shoucang_xuanzhongzhuangtai:before {
  content: "\e6a9";
}

.icon-jia1:before {
  content: "\e61c";
}

.icon-bangzhu1:before {
  content: "\e63d";
}

.icon-arrow-left-bottom:before {
  content: "\e602";
}

.icon-arrow-right-bottom:before {
  content: "\e603";
}

.icon-arrow-left-top:before {
  content: "\e604";
}

.icon-icon--:before {
  content: "\e744";
}

.icon-zuojiantou-up:before {
  content: "\e605";
}

.icon-xia:before {
  content: "\e62d";
}

.icon--jianhao:before {
  content: "\e60b";
}

.icon-weixinzhifu:before {
  content: "\e61a";
}

.icon-comment:before {
  content: "\e64f";
}

.icon-weixin:before {
  content: "\e61f";
}

.icon-fenlei1:before {
  content: "\e620";
}

.icon-erjiye-yucunkuan:before {
  content: "\e623";
}

.icon-Group-:before {
  content: "\e688";
}

.icon-you:before {
  content: "\e606";
}

.icon-forward:before {
  content: "\e607";
}

.icon-tuijian:before {
  content: "\e610";
}

.icon-bangzhu:before {
  content: "\e679";
}

.icon-share:before {
  content: "\e656";
}

.icon-yiguoqi:before {
  content: "\e997";
}

.icon-shezhi1:before {
  content: "\e61d";
}

.icon-fork:before {
  content: "\e61b";
}

.icon-kafei:before {
  content: "\e66a";
}

.icon-iLinkapp-:before {
  content: "\e654";
}

.icon-saomiao:before {
  content: "\e60d";
}

.icon-shezhi:before {
  content: "\e60f";
}

.icon-shouhoutuikuan:before {
  content: "\e631";
}

.icon-gouwuche:before {
  content: "\e609";
}

.icon-dizhi:before {
  content: "\e614";
}

.icon-fenlei:before {
  content: "\e706";
}

.icon-xingxing:before {
  content: "\e70b";
}

.icon-tuandui:before {
  content: "\e633";
}

.icon-zuanshi:before {
  content: "\e615";
}

.icon-zuo:before {
  content: "\e63c";
}

.icon-shoucang2:before {
  content: "\e62e";
}

.icon-shouhuodizhi:before {
  content: "\e712";
}

.icon-yishouhuo:before {
  content: "\e71a";
}

.icon-dianzan-ash:before {
  content: "\e617";
}

.clamp {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  display: block;
}

.goods-list {
  .title {
    font-size: 18px;
    color: #303133;
    line-height: 45px;
  }
  .clamp {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    display: block;
  }
  .title2 {
    font-size: 13px;
    color: #909399;
    line-height: 22px;
    height: 45px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: block;
  }
}

/* 全局样式 */
page {
  --status-bar-height: 0px;
  --top-window-height: 0px;
  --window-top: 0px;
  --window-bottom: 0px;
  height: 100%;
}

/* 修复提示框图标不居中问题 */
.uni-modal .uni-modal__bd {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 30rpx 20rpx !important;
}

.uni-modal .uni-modal__ft {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 15rpx 0 !important;
}

.uni-toast {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 25rpx !important;
}

.uni-toast__icon {
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 15rpx !important;
}

/* H5环境下的提示框样式 */
/* #ifdef H5 */
.uni-modal {
  padding: 15px !important;
}

.uni-modal__bd {
  padding: 20px 15px !important;
}

.uni-modal__title {
  padding-bottom: 10px !important;
}

.uni-modal__ft {
  margin-top: 15px !important;
}
/* #endif */

/* APP环境下的提示框样式 */
/* #ifdef APP-PLUS */
.uni-modal-dialog {
  min-height: auto !important;
}

.uni-modal-dialog-content {
  min-height: auto !important;
  padding: 30rpx 20rpx !important;
}

.uni-modal-dialog-title {
  padding: 10rpx 0 !important;
  font-size: 36rpx !important;
}

.uni-modal-dialog-text {
  padding: 20rpx 10rpx !important;
  font-size: 28rpx !important;
  line-height: 1.5 !important;
}

.uni-modal-dialog-button-group {
  height: auto !important;
  min-height: 90rpx !important;
  max-height: 100rpx !important;
}

.uni-modal-dialog-button {
  height: auto !important;
  min-height: 70rpx !important;
  max-height: 90rpx !important;
  line-height: 70rpx !important;
  font-size: 30rpx !important;
}
/* #endif */

/* 修改底部导航栏样式，使其更有活力 */
.uni-tabbar {
  display: none !important;
  height: 0 !important;
  padding: 0 !important;
  visibility: hidden !important;
  pointer-events: none !important;
  position: absolute !important;
  opacity: 0 !important;
  overflow: hidden !important;
}

.uni-tabbar-border {
  display: none !important;
}

.uni-app--showtabbar uni-page-wrapper::after {
  height: 0 !important;
}

/* 解决图标选中问题 */
.uni-tabbar__item.uni-tabbar__item--selected .uni-tabbar__icon {
  display: none !important;
}

.uni-tabbar__item-text {
  display: none !important;
}

/* 解决文字选中问题 */
.uni-tabbar__item.uni-tabbar__item--selected .uni-tabbar__item-text {
  display: none !important;
}

/* 添加选中项突起的白色圆圈 */
.uni-tabbar__item.uni-tabbar__item--selected::before {
  display: none !important;
}

/* 修复底部页面内容溢出问题 */
.page-container {
  height: 100%;
  overflow: hidden;
}

/* 修改搜索框的背景色 */
.up-search, .up-search__box, .uni-searchbar, .uni-searchbar__box {
  background-color: rgba(255, 255, 255, 0.6) !important;
}

/* 修正uview-plus导航栏背景色问题 */
.u-navbar,
.u-navbar .u-navbar__content,
.u-navbar--fixed {
  background-color: transparent !important;
}

/* 使导航栏背景色正确继承父元素颜色 */
.u-navbar .u-navbar__content {
  background: inherit !important;
}

/* 确保设置的背景色能正确应用 */
.search-header .u-navbar__content {
  background: linear-gradient(180deg, #e84d1c 0%, #f06e44 100%) !important;
}

/* #ifndef APP-NVUE */
@import '@/static/scss/global.scss';

/*每个页面公共css */
page {
  /* 确保内容区域不被底部tabbar遮挡 */
  padding-bottom: 120rpx;
  box-sizing: border-box;
}

/* 解决tabbar在某些情况下不显示的问题 */
uni-tabbar,
.uni-tabbar {
  z-index: 9999 !important;
}

/* APP-PLUS平台tabbar适配 */
/* #ifdef APP-PLUS */
.uni-app--showtabbar uni-page-wrapper::after {
  height: calc(100rpx + env(safe-area-inset-bottom));
}
/* #endif */

/* 解决图片显示问题 */
image {
  will-change: transform;
}

/* 修复svg在不同平台下的兼容性问题 */
svg {
  /* 解决某些平台下svg可能无法正常显示的问题 */
  overflow: visible;
  vertical-align: middle;
}

/* #endif */
</style>
